package com.bread.tliaswebmanagement.controller;

import com.bread.tliaswebmanagement.pojo.Result;
import com.bread.tliaswebmanagement.utils.AliyunOSSOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Slf4j
@RestController
public class UploadController {

    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;

    public UploadController(AliyunOSSOperator aliyunOSSOperator) {
        this.aliyunOSSOperator = aliyunOSSOperator;
    }

    @PostMapping("/uploadToLocal")
    public Result uploadToLocal(String name, Integer age, MultipartFile file) throws IOException {
        log.info("上传文件：{},{},{}", name, age, file);
        String originalFilename = file.getOriginalFilename();
        String newFileName = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
        file.transferTo(new File("D:\\Bread\\College\\Java\\Code\\JavaWebLearning\\web-ai-actualCombat\\tlias-web-management\\src\\main\\java\\images\\"+ newFileName));
        return Result.success();
    }

    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception {
        log.info("上传文件：{}", file.getOriginalFilename());
        //将文件上传到阿里云OSS
        String url = aliyunOSSOperator.upload(file.getBytes(), file.getOriginalFilename());
        log.info("文件上传成功，url：{}", url);
        return Result.success(url);
    }

}
